package com.csap.framework.oauth2.client;

import cn.dev33.satoken.oauth2.model.AccessTokenModel;
import com.csap.framework.annotation.ResponseAdvice;
import com.csap.framework.annotation.apidoc.Api;
import com.csap.framework.annotation.apidoc.ApiOperation;
import com.csap.framework.annotation.apidoc.ApiProperty;
import com.csap.framework.annotation.apidoc.ApiPropertys;
import com.csap.framework.extension.result.ResultParam;
import com.csap.framework.oauth2.UserInfo;
import com.csap.framework.oauth2.client.rest.Oauth2ClientApi;
import com.csap.framework.oauth2.request.MobileLoginRequest;
import com.csap.framework.oauth2.request.RegisterRequest;
import com.csap.framework.oauth2.request.TokenRequest;
import com.csap.framework.oauth2.request.UpdateRequest;
import com.csap.framework.oauth2.response.RegisterResponse;
import com.csap.framework.properties.apidoc.ParamType;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;

/**
 * 客户端接口
 *
 * @author yangchengfu
 * @dataTime 2021年-07月-29日 16:47:00
 **/
@RestController
@ResponseAdvice
@RequestMapping("client")
@Api(value = "客服端-授权认证")
@Slf4j
@AllArgsConstructor
public class Oauth2ClientEndpoint {
    /**
     * 客户端API
     */
    private Oauth2ClientApi oauth2ClientApiRest;

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @ApiOperation(value = "获取用户信息")
    @RequestMapping("getUserInfo")
    public ResultParam<UserInfo> getUserinfo() {
        return oauth2ClientApiRest.getUserinfo();
    }

    /**
     * 模式一，模式三，模式四：Code授权码 获取 Access-Token
     *
     * @param tokenRequest 授权数据
     * @return 返回token
     */
    @ApiOperation(value = "模式一，模式三，模式四：Code授权码 获取 Access-Token", description = "" +
            "模式一：Code授权码 获取 Access-Token" +
            "Refresh-Token 刷新 Access-Token" +
            "模式三：密码式" +
            "模式四：凭证式",
            paramType = ParamType.FORM_DATA)
    @RequestMapping("/oauth2/token")
    public ResultParam<AccessTokenModel> token(TokenRequest tokenRequest) {
        return oauth2ClientApiRest.token(tokenRequest);
    }

    @ApiOperation(value = "根据opendId删除用户", paramType = ParamType.QUERY)
    @ApiPropertys({
            @ApiProperty(value = "用户的openId", name = "openId", required = true),
            @ApiProperty(value = "客户端ID", name = "clientId", required = true)
    })
    @PostMapping("/oauth2/removeByOpenId")
    public ResultParam<Boolean> removeByOpenId(@RequestParam @NotNull(message = "openId不能为空") String openId, String platform, String loginType, String userType) {
        return oauth2ClientApiRest.removeByOpenId(openId);
    }

    /**
     * 普通注册
     *
     * @param registerRequest 注册信息
     * @return 注册结果
     */
    @ApiOperation(value = "注册", paramType = ParamType.BODY)
    @PostMapping("register")
    public ResultParam<RegisterResponse> register(@RequestBody RegisterRequest registerRequest) {
        return oauth2ClientApiRest.register(registerRequest);
    }

    /**
     * 一键登录只支持账号密码
     *
     * @param registerRequest 注册信息/登录
     * @return 登录结果
     */
    @ApiOperation(value = "一键登录只支持账号密码", paramType = ParamType.BODY)
    @PostMapping("loginRegister")
    public ResultParam<AccessTokenModel> loginRegister(@RequestBody RegisterRequest registerRequest) {
        return oauth2ClientApiRest.loginRegister(registerRequest);
    }

    /**
     * 手机号码
     *
     * @param mobileRequest 手机号登陆参数
     * @return 返回登录的token
     */
    @ApiOperation(value = "手机号码", paramType = ParamType.BODY)
    @PostMapping("loginMobile")
    public ResultParam<AccessTokenModel> loginMobile(@RequestBody MobileLoginRequest mobileRequest) {
        return oauth2ClientApiRest.loginMobile(mobileRequest);
    }

    /**
     * 根据OpendId修改用户信息
     *
     * @param updateRequest 修改的参数
     * @return 是否修改成功
     */
    @PutMapping("updateByOpenId")
    @ApiOperation(value = "根据OpendId修改用户信息", paramType = ParamType.BODY)
    public ResultParam<Boolean> updateByOpenId(@RequestBody UpdateRequest updateRequest) {
        return oauth2ClientApiRest.updateByOpenId(updateRequest);
    }

    /**
     * 退出登录
     */
    @ApiOperation(value = "退出登录")
    @RequestMapping("loginOut")
    public void loginOut() {
        oauth2ClientApiRest.loginOut();
    }
}
